package models;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileWriter;
import java.util.*;

import javax.imageio.ImageIO;
import javax.persistence.*;

import play.data.validation.MaxSize;
import play.data.validation.Required;
import play.db.DB;
import play.db.jpa.*;
 
@Entity
public class Carte extends Model
{
	@Required
	@MaxSize(50)
	public String libelle_carte;

	public Carte(String libelle_carte) 
	{
		this.libelle_carte = libelle_carte;
	}
	
	public String toString()
	{
		return this.libelle_carte;
	}
	
	// Crée les coordonnées associées à la nouvelle carte
	public String creerCoordonnees(File file, int precision)
	{
		long first_id = Coordonnee.getLastId();
		Condition_meteo cond = Condition_meteo.all().first();
		BufferedImage image;
		String texte = null;
	    
		try 
		{
			image = ImageIO.read(file);
			int height = image.getHeight();
			int width = image.getWidth();
			
			File carte_texte = new File(this.libelle_carte +".txt");
			FileWriter writer = new FileWriter(this.libelle_carte +".txt", true);
			
			for(int i = 1 ; i < height ; i+=precision)
			{
				for(int j = 1 ; j < width ; j+=precision)
				{
					int clr		=  image.getRGB(j,i); 
					int red   	= (clr & 0x00ff0000) >> 16;
					int green 	= (clr & 0x0000ff00) >> 8;
					int blue  	=  clr & 0x000000ff;
					
					// Si c'est de la terre
					/*
					if(red < 127 && green < 127 && blue < 127) {
						texte = ++first_id + "," + 0 + "," + j + "," + i + "," + this.id + "," + cond.id + "," + "\n";
					}
					else {
						texte = ++first_id + "," + 1 + "," + j + "," + i + "," + this.id + "," + cond.id + "," + "\n";
					}*/
					if(red > 200 && green > 200 && blue > 200) {
						texte = ++first_id + "," + 1 + "," + j + "," + i + "," + this.id + "," + cond.id + "," + "\n";
					}
					else {
						texte = ++first_id + "," + 0 + "," + j + "," + i + "," + this.id + "," + cond.id + "," + "\n";
					}					
			        writer.write(texte);
				}
			}
			writer.close();
			
	        String sql = " LOAD DATA LOCAL INFILE '" + carte_texte + "' REPLACE " +
	        "INTO TABLE coordonnee " +
	        "FIELDS TERMINATED BY ',' " +
	        "LINES TERMINATED BY '\\n'";	
	        DB.execute(sql);	
	        carte_texte.delete();
		} 
		
		catch (Exception e) {
			return "Problème lors de l'insertion des données";
		} 
		return null;
	}
	
	
	// Génère la météo de la carte
	public Condition_meteo genererMeteo()
	{
		Coordonnee ancienne_coord = Coordonnee.find("byCarte_id", this.id).first();
		Condition_meteo ancienne_cond = ancienne_coord.condition;
		
		double vent_augmente = (Math.random()*2);
		double direction_augmente = (Math.random()*2);
		float force_vent = 0;
		float direction_vent = 0;
		
		if(vent_augmente > 1)
			force_vent = (float) ((Math.round((Math.random()*12)))/3 + ancienne_cond.force_vent)%12;	
		else
			force_vent = (float) ((Math.round((Math.random()*12)))/3 - ancienne_cond.force_vent)%12;
		
		if(direction_augmente > 1)
			direction_vent = (float) ((Math.round((Math.random()*359)))/4 + ancienne_cond.direction_vent)%360;
		
		else
			direction_vent = (float) ((Math.round((Math.random()*359)))/4 - ancienne_cond.direction_vent)%360; 
		
		if(direction_vent < 0)
			direction_vent = -direction_vent;
		
		if(force_vent < 0)
			force_vent = -force_vent;
		
		
		Condition_meteo condition = new Condition_meteo(direction_vent, force_vent, new Date());
		Coordonnee coord = Coordonnee.find("byCarte_id", this.id).first();
		
		condition.save();
		coord.condition = condition;
		coord.save();
		return condition;
	}	
}
